/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.ctrl.mml;

/**
 *
 * @author Mr.Sam
 */
import java.util.*;

/**
* A class for the representation of an tag element (XML element).<BR>
* By default, the class represents a start empty tag.
*
* @author © 2000 DIRAT Laurent
* @version 1.0 25/05/2000
*/
public class Tag {
    /**
    * The name of the tag.
    */
    private String name;
    
    /**
    * The list of attributes.
    */
    private Vector attributes = new Vector();
    
    /**
    * Indicates if this is an empty element or not.<BR>
    * By default, it is an empty element.
    */
    private boolean isEmpty = true;
    
    /**
    * Indicates if it is a start tag or an end tag.<BR>
    * By default, it is a start element.
    */
    private boolean isStart = true;
    
    /**
    * The default constructor.<BR>
    * The default is an empty element, with "name" name and no attributes
    */
    public Tag() {
        this("name", true, true);
    }
    
    /**
    * The constructor.
    * Constructs an empty element with the specified name.
    * @param name the name of the element
    */
    public Tag(String name) {
        this(name, true, true);
    }
    
    /**
    * The constructor.
    * @param name the name of the element
    * @param isStart <CODE>true</CODE> if the tag is an start one. <CODE>false</CODE> otherwise.
    * @param isEmpty <CODE>true</CODE> if the element is an empty one. <CODE>false</CODE> otherwise.
    */
    public Tag(String name, boolean isStart, boolean isEmpty) {
        this.name = name;
        this.isStart = isStart;
        if (!isStart)
            this.isEmpty = false; // Si balise de fin alors forcément c'est pas un elément vide
        else
            this.isEmpty = isEmpty;
    }
    
    
    /**
    * Sets the name of the element.
    * @param name the name of the element.
    */
    public void setName(String name) {
        this.name = name;
    }
    
    /**
    * Returns the name of the element.
    */
    public String getName() {
        return name;
    }
    
    /**
    * Adds an attribute to the element.
    * @param att the attribute to add.
    */
    public void addAttribute(Attribute att) {
        attributes.addElement(att);
    }
    
    /**
    * Returns <CODE>true</CODE> if the element is an empty one. <CODE>false</CODE> otherwise.
    */
    public boolean isEmpty() {
        return isEmpty;
    }
    
    /**
    * Sets the element as empty.
    * @param isEmpty <CODE>true</CODE> if the element is an empty one. <CODE>false</CODE> otherwise.
    */
    public void setIsEmpty(boolean isEmpty) {
        this.isEmpty = isEmpty;
    }
    
    /**
    * Returns <CODE>true</CODE> if the tag is an start one. <CODE>false</CODE> otherwise.
    */
    public boolean isStart() {
        return isStart;
    }
    
    /**
    * Sets the element as start element.
    * @param isStart <CODE>true</CODE> if the element is an start one. <CODE>false</CODE> otherwise.
    */
    public void setIsStart(boolean isStart) {
        this.isStart = isStart;
    }
    
    /**
    * Returns <CODE>true</CODE> if the instance if the end tag of the specified one. <CODE>false</CODE>
    * otherwise.
    * @param tag the specified start tag
    */
    public boolean isEndTag(Tag start) {
        boolean isEndTag = false;
        
        isEndTag = (!isStart && start.isStart && name.equals(start.name));
        
        return isEndTag;
    }
    
    /**
    * Returns the string representation of the element.
    */
    public String toString() {
        String toString = "<";
        
        if (isStart) {
            toString += name;
            
            int count = attributes.size();
            for (int i = 0; i < count; i++) 
                toString += " " + ((Attribute) attributes.elementAt(i)).toString();
                
            if (isEmpty)
                toString += "/";
        }
        else
            toString += "/" + name;
        
        toString += ">";
        
        return toString;
    }
}
    